//
//  SkillVC.m
//  EVETracker
//
//  Created by Scott Guyer on 7/30/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Skill.h"
#import "Character.h"
#import "SkillVC.h"
#import "DateUtility.h"
#import "defs.h"



@implementation SkillVC

@synthesize skillText, trainingText, skillTitle;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
	}
	return self;
}


- (void)viewDidLoad 
{
	NSLog(@"%s", __FUNCTION__);
	
	UIFont* f = [trainingText.font fontWithSize:13];
	trainingText.font = f;
	f = [skillText.font fontWithSize:14];
	skillText.font = f;

}


- (void) setViewMode:(SkillViewMode)mode
{
	if ( mode == SkillViewModeBasic )
	{
		trainingText.hidden = YES;
		
		CGRect tf = trainingText.frame;
		CGRect pf = self.view.frame;
		
		CGRect f = CGRectMake( 20, tf.origin.y, 
							  pf.size.width - 40,
							  pf.size.height - tf.origin.y - 20);
		
		skillText.frame = f;		
	}
	else
	{
		trainingText.hidden = NO;
		
		CGRect tf = trainingText.frame;
		CGRect pf = self.view.frame;
		
		CGRect f = CGRectMake( 20, tf.origin.y + tf.size.height + 10, 
							  pf.size.width - 40,
							  pf.size.height - tf.origin.y - tf.size.height - 74 );
		
		skillText.frame = f;					
	}
}


- (void) showPilotViewFor:(Character*)pilot withSkill:(Skill*)skill 
{
	[self setViewMode:SkillViewModeTraining];

	int currentLevel = skill.level;
	
	NSString* title = [NSString stringWithFormat:@"%@ %d", skill.name, currentLevel];
	skillTitle.text = title;
	self.navigationItem.title = title;
	skillText.text = skill.description;	
	
	float sph = [pilot skillPointsPerHourWithPrimary:skill.primaryAttribute andSecondary:skill.secondaryAttribute];
	
	NSMutableString* buf = [[NSMutableString alloc] initWithCapacity:256];
	
	NSString* priStr = [Character nameForAttribute:skill.primaryAttribute];
	NSString* secStr = [Character nameForAttribute:skill.secondaryAttribute];
	[buf appendFormat:@"Training Rate: %d (%@/%@)\n", (int)ceil(sph), priStr, secStr];
	[priStr release];
	[secStr release];
	
	if ( currentLevel < 5 ) 
	{
		[buf appendFormat:@"Time to next levels: \n"];
	
		double lastSP = pow( 32.0, (currentLevel-1)/2.0);
		lastSP *= (250 * skill.rank);
		
		NSTimeInterval timespan;
		double sp;
		for ( int i=currentLevel+1; i<=5; i++ )	
		{
			sp = pow( 32.0, (i-1)/2.0);
			sp *= (250 * skill.rank);
		
			if ( sp > 0.0 )
			{	
				timespan = (sp - lastSP) / (sph / 3600.0);	
				lastSP = sp;
				NSString* time = [DateUtility secondsToString:timespan];
				//NSLog(@"level %d, sp %f, span %f,  %@", i, sp, timespan, time);
				[buf appendFormat:@"  %d = %@\n", i, time];
				[time release];
			}
		}
	}
	
	trainingText.text = buf;
	[buf release]; // TODO: this SHOULD be fine!!!
}

- (void) showBasicViewForSkill:(Skill*)skill 
{
	[self setViewMode:SkillViewModeBasic];
	
	skillTitle.text = skill.name;	
	self.navigationItem.title = skill.name;
	skillText.text = skill.description;	
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void) jenny 
{
	NSLog(@"%s", __FUNCTION__);
	// nothing to do at the moment
}

- (void)didReceiveMemoryWarning 
{
}


- (void)dealloc {
	NSLog(@"%s 0x%x", __FUNCTION__, self);

	WIPE( skillTitle)
	WIPE( skillText)
	WIPE( trainingText)
	
	[super dealloc];
}


@end
